package cn.ecut.controller;

import cn.ecut.model.Alumni;
import cn.ecut.model.Message;
import cn.ecut.model.Student;
import cn.ecut.service.AlumniService;
import com.mysql.cj.protocol.MessageSender;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

@Controller
@RequestMapping("/alumni")
public class AlumniController {
    @Autowired
    private AlumniService alumniService;

    @ResponseBody
    @RequestMapping("/addAlumni")
    public Message addAlumni(Alumni alumni){
        Message message = new Message();
        int role1=alumniService.slectCountByphone(alumni.getPhone());
        int role2=alumniService.slectCountBymail(alumni.getMail());
        System.out.println(role1);
        System.out.println(role2);
        if(role1!=0){
            message.setFlag(false);
            message.setMsg("该手机号已存在");
        }else if(role2 !=0){
            message.setFlag(false);
            message.setMsg("该邮箱已存在");
        }else{
            alumniService.addAlumni(alumni);
            message.setFlag(true);
        }
        return message;
    }

    @ResponseBody
    @RequestMapping("/showAlumni")
    public HashMap<String, Object> showAlumni(int page, int limit) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        List<Alumni> list = new ArrayList<>();
        int offset=(page-1)*limit;
        list = alumniService.selectAllAlumniByPage(offset,limit);
        Integer count = alumniService.selectAllcount();
        map.put("code", 0);
        map.put("msg", "");
        map.put("count", count);
        map.put("data", list);//dao层查询返回list数据
        Iterator<Map.Entry<String, Object>> entries = map.entrySet().iterator();
        while (entries.hasNext()) {
            Map.Entry<String, Object> entry = entries.next();
            System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
        }
        return map;
    }

    @ResponseBody
    @RequestMapping("/updataAlumni")
    public Message upataAlumni(Alumni alumni){
        Message message = new Message();
        int role = alumniService.uodataalumni(alumni);
        if(role==1){
            message.setFlag(true);
        }else{
            message.setFlag(false);
            message.setMsg("修改失败");
        }
        return message;
    }

    @ResponseBody
    @RequestMapping("/deleteAlumni")
    public Message deleteAlumni(int aid){
        Message message = new Message();
        int role=alumniService.deleteAlumni(aid);
        if(role==1){
            message.setFlag(true);
        }else {
            message.setFlag(false);
            message.setMsg("删除失败");
        }
        return message;
    }

    @ResponseBody
    @RequestMapping("/selectAlumni")
    public HashMap<String, Object> selectAlumni(String searchResult,int page, int limit) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        List<Alumni> list = new ArrayList<>();
        int offset=(page-1)*limit;
        list = alumniService.advanceSelect(searchResult, offset, limit);
        Integer count = alumniService.advanceSelectCount(searchResult);
        map.put("code", 0);
        map.put("msg", "");
        map.put("count", count);
        map.put("data", list);//dao层查询返回list数据
        Iterator<Map.Entry<String, Object>> entries = map.entrySet().iterator();
        while (entries.hasNext()) {
            Map.Entry<String, Object> entry = entries.next();
            System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
        }
        return map;
    }
}
